<template>
	<view class="page">
		<view class="topwrap">
			<u-search bgColor="#fff"
			 v-model="keywords" :show-action="false" @search="keyUpKeywords" @clear="clearKewords"/>
			  <view  @click="showJg=true; hideKeyboard()">
			   <u--input :disabled="true"  :customStyle="{background:'#fff',marginTop:'30rpx',fontSize:'14px'}"
			   suffixIcon="arrow-down"
			     v-model="jgName"
				  placeholder="请选择机构"
				 />
			   </view>
			   <view class="addStatus" v-if="userInfo.auditStatus!=2"
			   @click="goPage('/subpkg/tdContent/tdContent',{deptId:addDeptId,subUserNum:subUserNum,wdtd:'false',isMyzzId:addIsMyzzId,shStatus:addStatus})"
			   ><view style="font-weight: bold;">
				   
				   {{isAddShow?'组织：'+addDeptName:'已加入机构'}}
			   </view>
			   <u-tag v-if="isAddShow"
			   :text="userInfo.auditStatus==0?'待审核':
			   userInfo.auditStatus==1?'通过':''" 
			   :type="userInfo.auditStatus==0?'warning':
			   userInfo.auditStatus==1?'success':''" 
			   plain plainFill></u-tag>
			   </view>
		</view>
		
		<mescroll-body @init="mescrollInit" :down="downOption" :up="upOption" @down="downCallback" @up="upCallback">
			<tdItem :dataList="dataList" @addTd='addTd' :wdtd="false"/>
		</mescroll-body>
		<!-- 机构选择 -->
		<u-picker
			:show="showJg"
			:columns="jgdataArr"
			keyName="deptName"
			@cancel="jgClose"
			@confirm="jgSelect"
			></u-picker>
		</view>
	</view>
</template>

<script>
	import {mapActions,mapState} from 'vuex'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
				mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				/**分页参数**/
				dataList: [],//团队分页数据
				deptId:'',
				downOption:{
					auto:false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption:{
					auto:false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量
					},
					noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 空空如也 ~', // 提示
						btnText: '去看看'
					}
				},
				/****/
				showJg:false,//是否显示机构列表
				jgdataArr:[],//机构列表数据
				jgName:'',//双向绑定机构数据
				userInfo:{},
				keywords:'',
				addDeptName:'',
				addDeptId:'',
				subUserNum:'',
				addIsMyzzId:false,
				addStatus:'',
				isAddShow:''
			
				
			};
		},
		computed:{
			...mapState('m_zzjg',['jgData']),
			//...mapState('m_login',['userInfo'])
		},
		onLoad() {
			//机构数据列表
			this.getjgListAction({data:{},success:res=>{
				this.jgdataArr.push(res)
			}})
			this.getwxUserAction({success:res=>{
				this.userInfo=res.data.data
			
			this.getzzListAction({data:{deptId:this.userInfo.dept.deptId},success:res=>{
				console.log(res.data.rows)
					if(uni.$u.test.isEmpty(res.data.rows)) {
						this.isAddShow=false;
						return false;
					}
					this.isAddShow=true;
					res.data.rows[0].Myzz={addIsMyzzId:false,addStatus:null}
					if(this.userInfo.auditStatus==2||this.userInfo.auditStatus==null){
							res.data.rows[0].Myzz.addStatus=5
						}else{
							res.data.rows[0].Myzz.addStatus=null
						}
						
						if(res.data.rows[0].deptId==this.userInfo.dept.deptId){
							res.data.rows[0].Myzz.addStatus=this.userInfo.auditStatus
							res.data.rows[0].Myzz.addIsMyzzId=true
						}
						
					
				this.addDeptName=res.data.rows[0].deptName
				this.addDeptId=res.data.rows[0].deptId
				this.subUserNum=res.data.rows[0].subUserNum
				this.addIsMyzzId=res.data.rows[0].Myzz.addIsMyzzId
				this.addStatus=res.data.rows[0].Myzz.addStatus
			}})
			}})
			//deptId=149&isMyzzId=true&shStatus=0
		},
		onShow() {
			this.dataList=[]
			this.mescroll.resetUpScroll()
		},
		methods:{
			//路由跳转
			goPage(url,obj){
				uni.$u.route(url,obj);
			},
			...mapActions('m_zzjg',['getjgListAction','getzzListAction','hyaddDeptAction']),
			...mapActions('m_login',['getwxUserAction']),
			addTd(e){
				this.getwxUserAction({success:res=>{
					this.userInfo=res.data.data
					if(!uni.$u.test.isEmpty(this.userInfo)){
						if(uni.$u.test.isEmpty(this.userInfo.userAuth)||this.userInfo.userAuth.auditStatus==2) return uni.$u.toast('您还不是会员')
						this.hyaddDeptAction({deptId:e.deptId,success:res=>{
							this.dataList=[]
							this.mescroll.resetUpScroll()
						}})
					}
					}})
					 
				
				// this.addTypeId=e.deptId
				// this.getzzListAction({data:{deptId:this.addTypeId},success:res=>{
				// 	this.addList=res.data.rows
				// }})
				
				
				
			},
			//查询组织名称
			keyUpKeywords(e){
				this.dataList=[]
				this.keywords=e
				this.mescroll.resetUpScroll()
			},
			//清空组织名称查询
			clearKewords(){
				this.dataList=[]
				//this.keywords=''
				this.mescroll.resetUpScroll()
			},
			//选择机构[确定]
			jgSelect(e){
				this.dataList=[]
				this.parentId=e.value[0].deptId
				this.mescroll.resetUpScroll()
				this.jgName=e.value[0].deptName
				this.showJg=false;
			},
			//取消选择机构[取消]
			jgClose(e){
				this.dataList=[]
				this.parentId=''
				this.mescroll.resetUpScroll()
				this.jgName=''
				this.showJg=false;
			},
			//隐藏软键盘
			hideKeyboard() {
				uni.hideKeyboard()
			},
			//分页上拉加载下拉刷新
			/*下拉刷新的回调 */
			downCallback() {
				this.dataList=[]
				this.mescroll.resetUpScroll()
			},
			
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				const params={
					pageSize:page.size,
					pageNum:page.num,
					parentId:this.parentId,
					deptName:this.keywords
				}
				this.getwxUserAction({success:res=>{
					console.log(res.data.data)
					this.userInfo=res.data.data
				if(!uni.$u.test.isEmpty(this.userInfo)){
				this.getzzListAction({data:params,success:res=>{
					this.getjgListAction({data:{deptId:params.parentId},success:res2=>{
						
						let list=[]
						
					if(uni.$u.test.isEmpty(this.parentId)){
						list=[...res.data.rows]
						if(this.userInfo.auditStatus!=2){
							this.jgdataArr[0].forEach(it=>{
								if(it.deptId==this.userInfo.dept.deptId){
									list=[it,...list]
								}
							})
						}
						
					}else{
						list=[...res2,...res.data.rows]
						if(this.userInfo.auditStatus!=2){
							this.jgdataArr[0].forEach(it=>{
								if(res2[0].deptId!=it.deptId){
									if(it.deptId==this.userInfo.dept.deptId){
										list=[it,...list]
									}
								}
							})
						}
						
					}
					
					
					
				
					list.forEach(item=>{
						item.Myzz={isMyzzId:false,shStatus:null}
					})
					
						//console.log(res1.data.data.auditStatus)//1审核通过，0待审核，2审核失败
						
						list.forEach(item=>{
							if(this.userInfo.auditStatus==2||this.userInfo.auditStatus==null){
								item.Myzz.shStatus=5
							}else{
								item.Myzz.shStatus=null
							}
							
							if(item.deptId==this.userInfo.dept.deptId){
								item.Myzz.shStatus=this.userInfo.auditStatus
								item.Myzz.isMyzzId=true
							}
							
						})
						
						
						
						
						
						
						if(page.num == 1) this.indexList=[]
						this.dataList=this.dataList.concat(list)
						
					this.mescroll.endBySize(res.data.total+1, res.data.total)
					}})
					
					
				}})
				
				}
				}})
			}
		}
	}
</script>

<style lang="scss" scoped>
.page{width: 100%;height: 100%;background-color: #f2f5fa;overflow: hidden;}
.topwrap{margin:30rpx;margin-bottom: 0;}
/deep/ .u-border{border-width: 0 !important;}
.addStatus{margin-top:15px;height:40px;background:#fff;padding:0 6px;display: flex;justify-content: space-between;align-items: center;color:#666}
</style>
